import pygame as pg
from random import randint
### 系统常量 ###
image_path = 'images/'
RUN = 1
PAUSE= 2 #暂停
SUCCESS = 3  # 胜利
FAILURE = 4 # 失败

### 移动的玩仔
### 初始化
class Wanzai:
    def __init__(self,pos):
        # 形象
        self.ig = pg.image.load(image_path+'qie_1.png')
        self.image = pg.transform.smoothscale(self.ig, (120, 140))
        # 位置
        self.rect = self.image.get_rect()
        # 初始位置
        self.rect.center = pos

    ## 跟随鼠标移动
    def Move(self,pos_x):
         self.rect.centerx = pos_x

## 创建金币
class Gold:
    def __init__(self,speed):
        ## 金币的形象
        raw_image  = pg.image.load(image_path + "zhadan1.png")
        self.image = pg.transform.smoothscale( raw_image,(60,60))
        self.rect = self.image.get_rect()

        ## 初始位置


        ## 下落速度
        self.speed = speed
        self.restart()
    ## 移动
    def move(self):
        self.rect.top = self.rect.top + self.speed

    ## 生成初始位置
    def restart(self):
        x = randint(0,800 - self.image.get_width())
        y = 0
        self.rect.topleft = x,y

### 创建 炸弹类
class Bomb:
    def __init__(self,speed):
        pass
class Game:
    ### 初始化 ###
    def __init__(self):
        self.score = 0
        pg.init()
        self.screen = pg.display.set_mode((800,600))
        pg.display.set_caption("接金币")
        self.b1 = pg.image.load(image_path+'bg.png')
        self.background = pg.transform.smoothscale(self.b1,(800,600))

        # ## 创建玩仔对象
        self.wanzai = Wanzai((400,435))

        ## 创建金币对象
        self.gold = Gold(1.3)


        ### 游戏状态 ###
        self.state = RUN

    def text_image(name, size, text, color):
        # 创建新的系统字体对象
        font = pg.font.SysFont(name, size)
        # 文本渲染
        image = font.render(text, True, color)
        return image
    ### 事件监听
    def listen(self):
        for event in pg.event.get():
            if event.type == pg.QUIT:
                exit()

    ### 状态控制
    def control(self):
        if self.state == RUN:
            self.screen.blit(self.background,(0,0))
            #
            self.screen.blit(self.wanzai.image,self.wanzai.rect)
            # ### 获取鼠标当前位置
            pos = pg.mouse.get_pos()
            ### 移动玩仔对象
            self.wanzai.Move(pos[0])

            ## 绘制金币对象
            self.screen.blit(self.gold.image,self.gold.rect)
            ## 移动金币对象
            self.gold.move()
            ## 获取金币是否移除了窗口
            self.gold.restart()
            ## 检查金币对应的矩形是否包含在玩仔对应的矩形内部、
            if self.wanzai.rect.contains(self.gold.rect):
                self.score = self.score + 1
                self.gold.restart()


            ### 绘制分数
            # image = self.text_image('simhei', 30, '分数' + str(self.score), (255, 0, 0))
            # self.screen.blit(image,(10,10))

            pg.display.update()
        elif self.state == PAUSE:
            pass

    ### 运行时
    def run(self):
        while True:
            self.listen()
            self.control()


game = Game()
game.run()